<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Owners extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('owners_model');
    }

    function index() { show_404(); }

    function view_edit($ID){

        $this->data['title'] = "Uredi vlasnika";     
        $this->data['post_action'] = "EDIT";     
        $this->data['post'] = $this->owners_model->view_edit_owner($ID);
        echo $this->load->view('owners/edit_owner',$this->data);

    }

    function preview()
    {
        $data['post'] = $this->owners_model->view_edit_owner();

        echo json_encode(array('html'=>$this->load->view('owners/preview_owner',$data,TRUE)));
    }

    function view_new(){

        $this->data['title'] = "Dodaj vlasnika";
        $this->data['post_action'] = "NEW";
        echo $this->load->view('owners/edit_owner',$this->data);
        //$this->template->build('owners/edit_owner',$this->data); 

    }

    function view_all(){
        $this->data['title'] = "Vlasnici";
        $this->data['owners'] = Model\Owners::all(); 
        //$this->firephp->log($this->data['all_posts']);
        $this->template->build('owners/view_all_owners',$this->data); 

    }

    function submit_post()
    {

        /*General*/
        $this->form_validation->set_rules('company_person', 'company / person', 'required'); 
        $this->form_validation->set_rules('owner_type[]', 'type', 'required');

        //2 type validation for company and person owner
        switch ($this->input->post('company_person')) {
            case 'company':
                $this->form_validation->set_rules('name', 'name', 'required');

                break;
            case 'person':
                $this->form_validation->set_rules('surname', 'surname', 'required');

                break;
        } 
        $this->form_validation->set_rules('tax_type', 'tax type', 'callback_dropdown_check'); 
        $this->form_validation->set_rules('address', 'address', 'required');
        $this->form_validation->set_rules('city', 'city', 'required');
        $this->form_validation->set_rules('telephone', 'telephone', 'required');       

        $this->form_validation->set_message('required','Please enter owner <b>%s</b>!');

        if($this->form_validation->run() == TRUE)
        {
            switch($_POST['post_action']){

                case 'NEW':     $result = $this->owners_model->create_owner();
                    break;

                case 'EDIT':    $result = $this->owners_model->update_post();
                    break;

            }

            echo json_encode($result);   

        }
        else
        {
            echo json_encode(array('action'=>FALSE, 'msg'=>validation_errors()));
        }    
    }

    public function dropdown_check($str)
    {
        if ($str == '0')
        {
            $this->form_validation->set_message('dropdown_check', 'Please choose owner <b>%s</b>!');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }


    function delete($post_ID){

        if($this->owners_model->delete_owner($post_ID)){
            $this->view_all();
        }else{
            echo 'Error ocured';
            $this->view_all();
        }

    }

}

    /* End of file Owners.php */
    /* Location: ./application/controllers/Owners.php */